package org.jcms.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Admin;
import org.jcms.model.Customer;
import org.jcms.model.Manager;
import org.jcms.model.PageBean;
import org.jcms.service.AdminServiceI;
import org.jcms.service.CustomerServiceI;
import org.jcms.service.ManagerServiceI;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 得到角色的分页列表，返回json数据
 * 
 * 参数：
 * 
 * page：页号，从1开始
 * 
 * rows：行数，一页的数量
 * 
 * IDLike :模糊查找的ID 可以不传
 * 
 * type 要返回查找的角色类别 1：学生、教师    2或3：教材发行/仓库采购  4：管理员
 * 
 * @author zzy
 * 
 */
public class AdminGetAccounts extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AdminGetAccounts() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		AdminServiceI adminServiceI = (AdminServiceI) ac
				.getBean("adminServiceImpl");
		CustomerServiceI customerServiceI = (CustomerServiceI) ac
				.getBean("customerServiceImpl");
		ManagerServiceI managerServiceI = (ManagerServiceI) ac
				.getBean("managerServiceImpl");

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String IDLike = request.getParameter("IDLike");
		String type = request.getParameter("type");
		String retJson = "";
		int intPage = 1;
		int intRows = 10;

		JSONObject jsonObject = new JSONObject();

		try {
			int Itype = Integer.parseInt(type);
			intPage = Integer.parseInt(page);
			intRows = Integer.parseInt(rows);
			if (intPage <= 0) {
				intPage = 1;
			}
			if (intRows > 200) {
				intRows = 10;
			}

			switch (Itype) {
			case 1:
				List<Customer> customers = null;
				PageBean<Customer> pbCust = null;
				if (IDLike == null)
					pbCust = customerServiceI.findByPage("from Customer",
							intPage, intRows);
				else
					pbCust = customerServiceI.findByPage(
							"from Customer as t1 where t1.customerId like ?",
							intPage, intRows, "%" + IDLike + "%");
				jsonObject.put("total", pbCust.getTotalRows());
				customers = pbCust.getData();
				List<HashMap<String, String>> customerls = new ArrayList<HashMap<String, String>>();
				for (Customer customer : customers) {
					HashMap<String, String> tempMap = new HashMap<String, String>();
					tempMap.put("ID", customer.getCustomerId());
					tempMap.put("name", customer.getCustomerName());
					tempMap.put("isbaned",
							customer.getCustomerIsBlocked() == 0 ? "否" : "是");
					tempMap.put("type", customer.getCustomerType() == 0 ? "教师"
							: "学生");
					customerls.add(tempMap);
				}
				jsonObject.put("rows", customerls);
				retJson = jsonObject.toString();
				break;
			case 2:
			case 3:
				List<Manager> managers = null;
				PageBean<Manager> pbMana = null;
				if (IDLike == null)
					pbMana = managerServiceI.findByPage("from Manager",
							intPage, intRows);
				else
					pbMana = managerServiceI.findByPage(
							"from Manager as t1 where t1.managerId like ?",
							intPage, intRows, "%" + IDLike + "%");

				jsonObject.put("total", pbMana.getTotalRows());
				managers = pbMana.getData();
				List<HashMap<String, String>> managerls = new ArrayList<HashMap<String, String>>();
				for (Manager manager : managers) {
					HashMap<String, String> tempMap = new HashMap<String, String>();
					tempMap.put("ID", manager.getManagerId());
					tempMap.put("name", manager.getManagerName());
					tempMap.put("isbaned",
							manager.getManagerIsBlocked() == 0 ? "否" : "是");
					tempMap.put("type",
							manager.getManagerType() == 0 ? "教材发行人员" : "仓库采购人员");
					managerls.add(tempMap);
				}
				jsonObject.put("rows", managerls);
				retJson = jsonObject.toString();

				break;
			case 4:
				List<Admin> admins = null;
				PageBean<Admin> pbAdmin = null;
				if (IDLike == null)
					pbAdmin = adminServiceI.findByPage("from Admin", intPage,
							intRows);
				else
					pbAdmin = adminServiceI.findByPage(
							"from Admin as t1 where t1.adminId like ?",
							intPage, intRows, "%" + IDLike + "%");

				jsonObject.put("total", pbAdmin.getTotalRows());
				admins = pbAdmin.getData();

				List<HashMap<String, String>> adminls = new ArrayList<HashMap<String, String>>();

				for (Admin admin : admins) {

					HashMap<String, String> tempMap = new HashMap<String, String>();
					tempMap.put("ID", admin.getAdminId());
					tempMap.put("name", admin.getAdminName());
					tempMap.put("isbaned", admin.getAdminIsBlocked() == 0 ? "否"
							: "是");
					adminls.add(tempMap);
				}
				jsonObject.put("rows", adminls);
				retJson = jsonObject.toString();
				break;

			default:

				break;
			}

		}

		catch (Exception e) {

		}

		finally {
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
